<?php 

# -- DÉBUT NOTE DE LICENCE -------------------------------------
#
# Ce fichier fait partie de Clicpage.
#
# Copyright (c) 2010 Francois (www.clicpage.net)
# Distribué selon les termes de la licence GPL version 2.0.
# Consultez le fichier LICENCE ou
# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
#
# Ce fichier a été créé le 1 septembre 2010 et est inspiré
# de Clearbricks.
#
# Note de licence de Clearbricks:
# Copyright (c) 2003-2010 Olivier Meunier & Association Dotclear
# Clearbricks est distribué selon les termes de la licence GPL
# version 2.0.
#
# -- FIN NOTE DE LICENCE ---------------------------------------

class cpUrlHandler extends urlHandler
{
	
	public function getDocument()
	{
		
		$type = $args = '';
		
		if ($this->mode == 'path_info')
		{
			$part = substr($_SERVER['PATH_INFO'],1);
		}
		else
		{

			if(strpos($_SERVER['QUERY_STRING'], '?') !== false) {
				$req = explode('?', $_SERVER['QUERY_STRING']);	
				$part = substr($req[0], 1);
				$qs = $req[1];
			} elseif(strpos($_SERVER['QUERY_STRING'], '&') !== false) {
				$pos = strpos($_SERVER['QUERY_STRING'], '&');
				$part = substr($_SERVER['QUERY_STRING'], 1, $pos-1);
				$qs = substr($_SERVER['QUERY_STRING'], $pos+1);
			}
			else {
				$part = substr($_SERVER['QUERY_STRING'], 1);
				$qs = '';
			}
			
			$qs = $this->parseQueryString($qs);
			
			# Recreates some _GET and _REQUEST pairs
			if (!empty($qs))
			{
				foreach ($_GET as $k => $v) {
					if (isset($_REQUEST[$k])) {
						unset($_REQUEST[$k]);
					}
				}
				$_GET = $qs;
				$_REQUEST = array_merge($qs,$_REQUEST);
	
			}
		}
		
		$_SERVER['URL_REQUEST_PART'] = $part;
		
		$this->getArgs($part,$type,$args);
		
		if (!$type)
		{
			$this->type = 'default';
			$this->callDefaultHandler($args);
		}
		else
		{
			$this->type = $type;
			$this->callHandler($type,$args);
		}
	}
	
	protected function parseQueryString($qs)
	{		
		if (!empty($qs))
		{
			$q = explode('&',$qs);
			$T = array();
			foreach ($q as $v)
			{
				$t = explode('=',$v,2);
				
				$t[0] = rawurldecode($t[0]);
				if (!isset($t[1])) {
					$T[$t[0]] = null;
				} else {
					$T[$t[0]] = urldecode($t[1]);
				}
			}

			return $T;
		}
		return array();
	}
	
}

?>